package service.vo.XMLStruts;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * 一个action对应一个解析出来的XML类文件
 * 
 * @author admin
 *
 */
public class XMLBean {
	public XMLBean() {

	}

	private String beanName;

	private String path;

	private String actionType;

	private String actionClass;

	private String formClass;

	private Map<String, String> actionForward = new HashMap<String, String>();

	public static Map<String, XMLBean> excuteXML(String xmlPath) throws JDOMException,
			IOException {
		XMLBean xml = new XMLBean();
		SAXBuilder builder = new SAXBuilder();
		Map<String, XMLBean> XMlMap = new HashMap<String, XMLBean>();
		Document document = builder.build(new File(xmlPath));
		Element element = document.getRootElement();// 获取其xml文件所有的标签，
		Element actionEle = element.getChild("action-mapping");// 获取action-Mapping中的子标签，
		List<Element> actlistE = actionEle.getChildren();// 获取actionEle 标签下面的子标签
															// 所有元素，类是element
		for (int i = 0; i < actlistE.size(); i++) {
			Element actE = actlistE.get(i);
			String actName = actE.getAttributeValue("name");// 获取action
															// 标签name属性对应的值
			xml.setBeanName(actName);
			List<Element> formE = element.getChild("formbeans").getChildren();
			for (int j = 0; j < formE.size(); j++) {
				Element formAttr = formE.get(j);
				String formName = formAttr.getAttributeValue("name");// 获取formbeans中的name值\
				if (actName.equals(formName)) {
					String formClass = formAttr.getAttributeValue("class");
					xml.setFormClass(formClass);
					break;
				}
			}
			String path = actE.getAttributeValue("path");
			String actionType = actE.getAttributeValue("type");
			xml.setPath(path);
			xml.setActionClass(actionType);
			List<Element> forwardList = actE.getChildren();
			Map<String, String> map = new HashMap<String, String>();
			for (int j = 0; j < forwardList.size(); j++) {
				Element forwardEle = forwardList.get(j);
				String name = forwardEle.getAttributeValue("name");
				String forwPath = forwardEle.getAttributeValue("value");
				map.put(name, forwPath);
			}
			xml.setActionForward(map);
			XMlMap.put(path, xml);
			System.out.println("XMlMap:"+XMlMap);
		}
		return XMlMap;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public String getActionClass() {
		return actionClass;
	}

	public void setActionClass(String actionClass) {
		this.actionClass = actionClass;
	}

	public String getFormClass() {
		return formClass;
	}

	public void setFormClass(String formClass) {
		this.formClass = formClass;
	}

	public Map<String, String> getActionForward() {
		return actionForward;
	}

	public void setActionForward(Map<String, String> actionForward) {
		this.actionForward = actionForward;
	}

	@Override
	public String toString() {
		return "XMLBean [beanName=" + beanName + ", path=" + path
				+ ", actionType=" + actionType + ", actionClass=" + actionClass
				+ ", formClass=" + formClass + ", actionForward="
				+ actionForward + "]";
	}

	public static void main(String[] args) {
		try {
			new XMLBean().excuteXML("E:\\eclipsePro\\XMLText\\src\\bin\\struts.xml");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
